﻿/***********************************************************************
 * <copyright file="Mapper.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Saturday, March 29, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System.Linq;
using System.Collections.Generic;
using BusinessSecurity;
using SecurityService.DataTransferObjects;

namespace SecurityService.DataTransferObjectMapper
{
    /// <summary>
    /// Maps DTOs (Data Transfer Objects) to BOs (Business Objects) and vice versa.
    /// </summary>
    public static class Mapper
    {
        #region Site

        /// <summary>
        /// Froms the data transfer object.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public static Site FromDataTransferObject(SiteDto obj)
        {
            if (obj == null) return null;
            return new Site
            {
                Id = obj.Id,
                Title = obj.Title,
                Description = obj.Description,
                Controller = obj.Controller,
                Order = obj.Order,
                ParentId = obj.ParentId,
                Status = obj.Status,
                IsGroup = obj.IsGroup
            };
        }

        /// <summary>
        /// To the data transfer object.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public static SiteDto ToDataTransferObject(Site obj)
        {
            if (obj == null) return null;

            return new SiteDto
            {
                Id = obj.Id,
                Title = obj.Title,
                Description = obj.Description,
                Controller = obj.Controller,
                Order = obj.Order,
                ParentId = obj.ParentId,
                Status = obj.Status,
                IsGroup = obj.IsGroup
            };
        }

        /// <summary>
        /// To the data transfer objects.
        /// </summary>
        /// <param name="objs">The objs.</param>
        /// <returns></returns>
        public static IList<SiteDto> ToDataTransferObjects(IEnumerable<Site> objs)
        {
            return objs == null ? null : objs.Select(x => ToDataTransferObject((x))).ToList();
        }

        #endregion

        #region Permission

        /// <summary>
        /// To the data transfer objects.
        /// </summary>
        /// <param name="objs">The objs.</param>
        /// <returns></returns>
        public static IList<PermissionDto> ToDataTransferObjects(IEnumerable<Permission> objs)
        {
            return objs == null ? null : objs.Select(x => ToDataTransferObject((x))).ToList();
        }

        /// <summary>
        /// To the data transfer object.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public static PermissionDto ToDataTransferObject(Permission obj)
        {
            if (obj == null) return null;

            return new PermissionDto
            {
                Id = obj.Id,
                Title = obj.Title,
                Code = obj.Code,
                Description = obj.Description,
                Order = obj.Order,
                Status = obj.Status,
                ShowDefault = obj.ShowDefault,
                HasImage = obj.HasImage,
                MultiRow = obj.MultiRow
            };
        }

        /// <summary>
        /// Froms the data transfer object.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public static Permission FromDataTransferObject(PermissionDto obj)
        {
            if (obj == null) return null;

            return new Permission
            {
                Id = obj.Id,
                Title = obj.Title,
                Code = obj.Code,
                Description = obj.Description,
                Order = obj.Order,
                Status = obj.Status,
                ShowDefault = obj.ShowDefault,
                HasImage = obj.HasImage,
                MultiRow = obj.MultiRow
            };
        }

        #endregion

        #region Role
        /// <summary>
        /// Froms the data transfer object.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public static Role FromDataTransferObject(RoleDto dto)
        {
            if (dto == null) return null;

            return new Role
            {
                RoleId = dto.RoleId,
                Title = dto.Title,
                RoleName = dto.RoleName,
                Description = dto.Description,
                IsActive = dto.IsActive
            };
        }

        /// <summary>
        /// To the data transfer objects.
        /// </summary>
        /// <param name="roles">The roles.</param>
        /// <returns></returns>
        public static List<Role> ToDataTransferObjects(IEnumerable<RoleDto> roles)
        {
            return roles == null ? null : roles.Select(x => ToDataTransferObject((x))).ToList();
        }

        /// <summary>
        /// To the data transfer object.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public static Role ToDataTransferObject(RoleDto dto)
        {
            if (dto == null) return null;

            return new Role
            {
                RoleId = dto.RoleId,
                Title = dto.Title,
                RoleName = dto.RoleName,
                Description = dto.Description,
                IsActive = dto.IsActive
            };
        }

        /// <summary>
        /// To the data transfer objects.
        /// </summary>
        /// <param name="roles">The roles.</param>
        /// <returns></returns>
        public static IList<RoleDto> ToDataTransferObjects(IEnumerable<Role> roles)
        {
            return roles == null ? null : roles.Select(x => ToDataTransferObject((x))).ToList();
        }

        /// <summary>
        /// To the data transfer object.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static RoleDto ToDataTransferObject(Role entity)
        {
            if (entity == null) return null;

            return new RoleDto
            {
                RoleId = entity.RoleId,
                RoleName = entity.RoleName,
                Title = entity.Title,
                Description = entity.Description,
                IsActive = entity.IsActive
            };
        }

        #endregion

        #region RoleInSite
        /// <summary>
        /// To the data transfer objects.
        /// </summary>
        /// <param name="roles">The roles.</param>
        /// <returns></returns>
        public static List<RoleInSiteDto> ToDataTransferObjects(IEnumerable<RoleInSite> roles)
        {
            return roles == null ? null : roles.Select(x => ToDataTransferObject((x))).ToList();
        }

        /// <summary>
        /// Froms the data transfer objects.
        /// </summary>
        /// <param name="objs">The objs.</param>
        /// <returns></returns>
        public static List<RoleInSite> FromDataTransferObjects(List<RoleInSiteDto> objs)
        {
            return objs == null ? null : objs.Select(x => ToDataTransferObject((x))).ToList();
        }

        /// <summary>
        /// To the data transfer object.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public static RoleInSite ToDataTransferObject(RoleInSiteDto obj)
        {
            if (obj == null) return null;
            return new RoleInSite
            {
                RoleId = obj.RoleId,
                PermissionId = obj.PermissionId,
                SiteId = obj.SiteId
            };
        }

        /// <summary>
        /// To the data transfer object.
        /// </summary>
        /// <param name="role">The role.</param>
        /// <returns></returns>
        public static RoleInSiteDto ToDataTransferObject(RoleInSite role)
        {
            if (role == null) return null;

            return new RoleInSiteDto
            {
                RoleId = role.RoleId,
                PermissionId = role.PermissionId,
                SiteId = role.SiteId
            };
        }

        #endregion

        #region PermissionInSite
        /// <summary>
        /// To the data transfer object.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public static PermissionInSiteDto ToDataTransferObject(PermissionInSite obj)
        {
            if (obj == null) return null;
            return new PermissionInSiteDto
            {
                PermissionId = obj.PermissionId,
                SiteId = obj.SiteId
            };
        }

        /// <summary>
        /// To the data transfer object.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public static PermissionInSite ToDataTransferObject(PermissionInSiteDto obj)
        {
            if (obj == null) return null;
            return new PermissionInSite
            {
                PermissionId = obj.PermissionId,
                SiteId = obj.SiteId
            };
        }

        /// <summary>
        /// To the data transfer objects.
        /// </summary>
        /// <param name="objs">The objs.</param>
        /// <returns></returns>
        public static IList<PermissionInSiteDto> ToDataTransferObjects(IList<PermissionInSite> objs)
        {
            return objs == null ? null : objs.Select(x => ToDataTransferObject((x))).ToList();
        }

        /// <summary>
        /// Froms the data transfer object.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public static PermissionInSite FromDataTransferObject(PermissionInSiteDto obj)
        {
            if (obj == null) return null;
            return new PermissionInSite
            {
                PermissionId = obj.PermissionId,
                SiteId = obj.SiteId
            };
        }

        /// <summary>
        /// Froms the data transfer object.
        /// </summary>
        /// <param name="objs">The objs.</param>
        /// <returns></returns>
        public static List<PermissionInSite> FromDataTransferObjects(List<PermissionInSiteDto> objs)
        {
            return objs == null ? null : objs.Select(x => ToDataTransferObject((x))).ToList();
        }

        #endregion

        #region User

        /// <summary>
        /// To the data transfer objects.
        /// </summary>
        /// <param name="c">The c.</param>
        /// <returns></returns>
        public static IList<UserDto> ToDataTransferObjects(IEnumerable<User> c)
        {
            return c == null ? null : c.Select(source => ToDataTransferObject((source))).ToList();
        }

        /// <summary>
        /// To the data transfer object.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static UserDto ToDataTransferObject(User entity)
        {
            if (entity == null) return null;

            return new UserDto
            {
                UserId = entity.UserId,
                UserName = entity.UserName,
                Password = entity.Password,
                IsActive = entity.IsActive,
                IsApproved = entity.IsApproved,
                IsBlocked = entity.IsBlocked,
                FullName = entity.FullName,
                Email = entity.Email,
                LastActivityDate = entity.LastActivityDate,
                Address = entity.Address,
                CreateDate = entity.CreateDate,
                UnitId = entity.UnitId,
                Roles = ToDataTransferObjects(entity.Roles)
            };
        }

        /// <summary>
        /// Froms the data transfer object.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public static User FromDataTransferObject(UserDto dto)
        {
            if (dto == null) return null;

            return new User
            {
                UserId = dto.UserId,
                UserName = dto.UserName,
                Password = dto.Password,
                IsActive = dto.IsActive,
                IsApproved = dto.IsApproved,
                IsBlocked = dto.IsBlocked,
                FullName = dto.FullName,
                Email = dto.Email,
                Address = dto.Address,
                CreateDate = dto.CreateDate,
                UnitId = dto.UnitId,
                Roles = ToDataTransferObjects(dto.Roles)
            };
        }

        /// <summary>
        /// Froms the data transfer object.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public static RecoverPassword FromDataTransferObject(RecoverPasswordDto dto)
        {
            if (dto == null) return null;

            return new RecoverPassword
            {
                UserName = dto.UserName,
                Email = dto.Email,
                Verify = dto.Verify
            };
        }

        #endregion
    }
}
